package cn.math.appmath.action;

import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import cn.math.appmath.biz.DownloadBiz;
import cn.math.appmath.entity.Download;

import com.opensymphony.xwork2.ActionSupport;

public class FileDownloadAction extends ActionSupport {
	private static final long serialVersionUID = -4565898601842133546L;
	private Download download;
	private String fileName;
	private DownloadBiz downloadBiz;
	
	/**inputStreanm属性的getter方法，StreamResult结果类型
	 * 使用该属 性来读取下载文件的内容
	 * @return
	 */
	public InputStream getInputStream() throws Exception {
		Integer id = 0;
		String idStr = ServletActionContext.getRequest().getParameter("id");
		if(idStr != null && !"".equals(idStr)) {
			id = Integer.parseInt(idStr);
		}
		download = this.downloadBiz.findById(id);
		String name = download.getDurl();
		fileName = name.substring(name.lastIndexOf("/"));
		return ServletActionContext.getServletContext()
			.getResourceAsStream(download.getDurl());
	}
	
	public String execute() throws Exception {
		return SUCCESS;
	}

	public Download getDownload() {
		return download;
	}

	public void setDownload(Download download) {
		this.download = download;
	}

	public void setDownloadBiz(DownloadBiz downloadBiz) {
		this.downloadBiz = downloadBiz;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
}
